﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Denting
{
    public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();
            comboBox_impactor.SelectedIndex = 0;
            comboBox_SplitCount.SelectedIndex = 2;
            textSplitRadio.Enabled = false;
            comboBox_SplitCount.Enabled = false;
            textSplitRadio.Text = "15.0";
            comboBox_SplitCount.SelectedIndex = 2;
            ImpactorFileDirectory = Directory.GetCurrentDirectory() + "\\Impactors";
            label_BigMessge.Text = "版权声明：\r\n\r\n        为避免不必要的纠纷，本软件仅限在[北京希艾益科技有限公司]内部使用，未经授权禁止在其他场合使用。\r\n\r\n        版权和最终解释权归程序作者本人所有，程序使用过程中如出现问题请及时联系作者本人：\r\n\r\nEmail : weishusen@163.com\r\n\r\nTel : 15120088386";

            //测试版时间验证
            string timeEnd = "2020-11-30 23:59:59";
            DateTime dtEnd = Convert.ToDateTime(timeEnd);
            DateTime dtNow = DateTime.Now;
            Console.WriteLine(dtEnd.ToString());

            foreach (string s in Directory.GetFiles(ImpactorFileDirectory))
            {
                if (Path.GetFileName(s) != "D25mm.inp" && Path.GetFileName(s) != "D80mm.inp" && Path.GetFileName(s) != "Glue_D80mm.inp" && Path.GetFileName(s) != "Denting.chm")
                {
                    comboBox_impactor.Items.Add(Path.GetFileName(s));
                }
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "inp文件|*.inp";
            openFileDialog1.FileName = "HyperMesh导出的inp文件";
            openFileDialog1.ShowDialog();
            InpFileFullPath = openFileDialog1.FileName;
            //判断文件是否存在
            if (File.Exists(InpFileFullPath))
            {
                label_Help.Text = "已经打开了文件：" + InpFileFullPath;
                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
                groupBox3.Enabled = true;
                button_exportInpFile.Enabled = true;
                getCompAndNodeSetInfo();
            }
            
            
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
                textSplitRadio.Enabled = false;
                comboBox_SplitCount.Enabled = false;
                if (comboBox_impactor.SelectedIndex == 0)
                {
                    textSplitRadio.Text = "15.0";
                    comboBox_SplitCount.SelectedIndex = 2;
                }
                else if (comboBox_impactor.SelectedIndex == 1)
                {
                    textSplitRadio.Text = "40.0";
                    comboBox_SplitCount.SelectedIndex = 1;
                }
                else if (comboBox_impactor.SelectedIndex == 2)
                {
                    textSplitRadio.Text = "40.0";
                    comboBox_SplitCount.SelectedIndex = 1;
                }
        }

        private void comboBox_impactor_SelectedIndexChanged(object sender, EventArgs e)
        {
                if (comboBox_impactor.SelectedIndex == 0)
                {
                    textSplitRadio.Text = "15.0";
                    comboBox_SplitCount.SelectedIndex = 2;
                }
                else if (comboBox_impactor.SelectedIndex == 1)
                {
                    textSplitRadio.Text = "40.0";
                    comboBox_SplitCount.SelectedIndex = 1;
                }
                else if (comboBox_impactor.SelectedIndex == 2)
                {
                    textSplitRadio.Text = "40.0";
                    comboBox_SplitCount.SelectedIndex = 1;
                }
            
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            textSplitRadio.Enabled = true;
            comboBox_SplitCount.Enabled = true;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {

                textBox_nodeID.Enabled = true;
                comboBox_NodeSet.Enabled = false;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            textBox_nodeID.Enabled = false;
            comboBox_NodeSet.Enabled = true; 
        }

        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar<48 ||e.KeyChar>57) && e.KeyChar!=8)
            {
                e.Handled = true;
            }
        }

        private void compid_Validating(object sender, CancelEventArgs e)
        {
            HMComponent temp = new HMComponent();
            bool b = false;
            if (textBox_CompID.Text.Length==0)
            {
                label_Help.Text = "指定COMP_ID号不能为空";
                label_BigMessge.Text = "";
                label_Help.BackColor = Color.Red;
            } 
            else
            {
                foreach (HMComponent cp in ListHMComponent)
                {
                    if (cp.compID == Convert.ToInt32(textBox_CompID.Text))
                    {
                        temp = cp;
                        b = true;
                        break;
                    }
                }
                if (b == false)
                {
                    label_Help.Text = "指定COMP_ID号不存在，请重新输入";
                    label_BigMessge.Text = "";
                    label_Help.BackColor = Color.Red;
                }
                else if (b == true)
                {
                    label_Help.Text = "已指定component,名称为 ：" + temp.compName;
                    label_Help.BackColor = System.Drawing.SystemColors.Window;
                    label_BigMessge.Text = "抗凹分析的表面单元所处的COMPID已经指定完毕，请从左侧的【测试点选择】中添加测试点,所有步骤完成后点击按钮【批处理写出INP文件】完成所有操作";
                }
            }
            
        }

        private void KeyUp_lostfouce(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                SendKeys.Send("{TAB}"); 
            }
        }

        private void textSplitRadio_Validating(object sender, CancelEventArgs e)
        {
            if (textSplitRadio.Text.Length==0)
            {
                label_Help.Text = "指定的半径不能为空";
                label_Help.BackColor = Color.Red;
            }
            else if (Convert.ToDouble(textSplitRadio.Text) > 100 )
            {
                label_Help.Text = "指定的半径过大，超出合理范围，请设置小于100mm内的半径值";
                label_Help.BackColor = Color.Red;
            }
            else if (Convert.ToDouble(textSplitRadio.Text) < 5)
            {
                textSplitRadio.Text = Convert.ToDouble(textSplitRadio.Text).ToString();
                label_Help.Text = "指定的半径过小，超出合理范围，请设置大于5mm内的半径值";
                label_Help.BackColor = Color.Red;
            }
            else
            {
                ImpactorRadioInTextBox = Convert.ToDouble(textSplitRadio.Text);
                label_Help.Text = "已指定细化半径 " + Convert.ToDouble(textSplitRadio.Text) +" mm";
                label_Help.BackColor = System.Drawing.SystemColors.Window;
            }
        }

        private void textBox_rd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textSplitRadio.Text.Contains('.'))
            {
                if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
                {
                    e.Handled = true;
                }
            }
            else 
            {
                if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar!='.')
                {
                    e.Handled = true;
                }
            }
        }

        private void comboBox_NodeSet_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox_DentingNodeID.Items.Clear();
            label_Help.BackColor = System.Drawing.SystemColors.Window;
            label_Help.Text = "";
            foreach (HMNodeSet ns in ListHMNodeSet)
            {
                if (ns.nodeSetName==comboBox_NodeSet.Text)
                {
                    foreach(int nodeID in ns.listNode)
                    {
                        listBox_DentingNodeID.Items.Add(nodeID);
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label_Help.BackColor = System.Drawing.SystemColors.Window;
            label_Help.Text = "";
            if (textBox_nodeID.Text.Length>0)
            {
                listBox_DentingNodeID.Items.Add(Convert.ToInt32(textBox_nodeID.Text));
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            while (listBox_DentingNodeID.SelectedItems.Count!=0)
            {
                listBox_DentingNodeID.Items.Remove(listBox_DentingNodeID.SelectedItem);
            }
        }

        private void KeyUp_NodeID(object sender, KeyEventArgs e)
        {
            label_Help.BackColor = System.Drawing.SystemColors.Window;
            label_Help.Text = "";
            if (e.KeyCode == Keys.Enter)
            {
                if (textBox_nodeID.Text.Length!=0)
                {
                    listBox_DentingNodeID.Items.Add(Convert.ToInt32(textBox_nodeID.Text));
                    textBox_nodeID.Text = "";
                }
            }
        }


        private void comboBox_SplitCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            SplitCout = Convert.ToInt32(comboBox_SplitCount.Text);
        }

        private void button_exportInpFile_Click(object sender, EventArgs e)
        {
            bool canItRun = false;
            label_BigMessge.Text = "";

            

            if (textBox_CompID.Text.Length == 0)
            {
                label_Help.Text = "指定COMP_ID号不能为空";
                label_BigMessge.Text = "";
                label_Help.BackColor = Color.Red;
                canItRun = false;
            }
            else if (textSplitRadio.Text.Length == 0)
            {
                label_Help.Text = "指定的半径不能为空";
                label_Help.BackColor = Color.Red;
                canItRun = false;
            }
            else if (listBox_DentingNodeID.Items.Count==0)
            {
                label_Help.Text = "没有添加任何的测试点";
                label_Help.BackColor = Color.Red;
                canItRun = false;
            }
            else
            {
                foreach (HMComponent cp in ListHMComponent)
                {
                    if (cp.compID == Convert.ToInt32(textBox_CompID.Text))
                    {
                        canItRun = true;
                        break;
                    }
                }
                if (!canItRun)
                {
                    label_Help.Text = "指定COMP_ID号不存在，请重新输入";
                    label_BigMessge.Text = "";
                    label_Help.BackColor = Color.Red;
                }
            }


            if(canItRun)
            {
                CompIDOnDentingSurface = Convert.ToInt32(textBox_CompID.Text);
                SetEachStatic();
                WriteConstIncludeFile();
                Write_PartA_IncludeFile();

                //关键的执行代码，要不断完善
                List<int> nsToRun = new List<int>();

                foreach (int dentingNodeID in listBox_DentingNodeID.Items)
                {
                    if (IsNodeOnSurface(dentingNodeID))
                    {
                        nsToRun.Add(dentingNodeID);
                    }
                }


                if (nsToRun.Count == 0)
                {
                    label_Help.Text = "没有任何点在指定的ID上 - -！";
                }
                else
                {
                    foreach (int dentingNodeID in nsToRun)
                    {
                            //do something
                            DentingNode dt = new DentingNode(InpFileFullPath, dentingNodeID);
                            dt.ImpactorRadio = ImpactorRadioInTextBox;
                            dt.SplitCounts = SplitCout;
                            dt.Run();

                            label_BigMessge.Text += dentingNodeID.ToString() + "执行完毕\r\n\r\n";
                    }


                    label_BigMessge.Text += "生成了用于计算的结果文件，请在以下文件夹内查看：\r\n\r\n";
                    label_BigMessge.Text += OutputFileDirectory + "\r\n";

                    //创建批处理文件
                    StreamWriter sw = new StreamWriter(OutputFileDirectory + "\\ANALYSIS_ALL.bat");
                    foreach (string inp_path in Directory.GetFiles(OutputFileDirectory))
                    {
                        string s = Path.GetFileNameWithoutExtension(inp_path);
                        if (s != "ANALYSIS_ALL")
                        {
                            sw.WriteLine("call abaqus job={0} cpus=4 int", s);
                        }
                    }
                    sw.Close();

                    System.Diagnostics.Process.Start(OutputFileDirectory);

                }
                }

                
        }

        private void 帮助HToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(ImpactorFileDirectory + "\\Denting.chm");
        }
    }
}
